package com.newly.driver.sdk.device.video.service.impl.tencent;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.newly.driver.sdk.device.video.cloud.record.RecordItem;
import com.newly.driver.sdk.device.video.cloud.record.tencent.TXCreateTimeTempReq;
import com.newly.driver.sdk.device.video.cloud.record.tencent.TXCreateTimeTempResp;
import com.newly.driver.sdk.device.video.cloud.record.tencent.TXQueryTimeTempResp;
import com.newly.driver.sdk.device.video.cloud.record.tencent.TXTimeTemplateItem;
import com.newly.driver.sdk.device.video.exception.NewlyDeviceException;
import com.newly.driver.sdk.device.video.service.api.tencent.ITencentRecord;
import com.newly.driver.sdk.device.video.service.impl.AbstractCloud;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.iotvideoindustry.v20201201.IotvideoindustryClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author gjm guan
 * @date 2021/10/15 15:08
 * @desc 腾讯平台
 */
@Component
@Slf4j
public abstract class AbstractTencentCloud extends AbstractCloud {

    @Value("${device.tencent.secretId}")
    protected String secretId;

    @Value("${device.tencent.secretKey}")
    protected String secretKey;

    @Value("${device.tencent.host}")
    protected String host;

    @Value("${device.tencent.region}")
    protected String region;

    @Value("${device.tencent.record.default.time.full}")
    protected String timeTemplateId;

    @Autowired
    private ITencentRecord tencentRecord;
    /**
     * 录制中的设备
     */
    protected Map<String, RecordItem> recordingItems = new ConcurrentHashMap<>();

    /**
     * 腾讯云请求状态
     */
    public enum TencentRequestState {
        OK(1, "OK"), ERR(2, "ERR");

        TencentRequestState(int key, String value) {
            this.key = key;
            this.value = value;
        }

        public int getKey() {
            return key;
        }

        public String getValue() {
            return value;
        }

        private int key;

        private String value;
    }

    /**
     * 腾讯云设备状态
     * 设备状态；0：设备不在线；1：设备在线；2：设备隔离中；3：设备未注册
     */
    public enum TencentDeviceState {
        OFFLINE(0, "设备不在线"), ONLINE(1, "设备在线"),
        ISOLATION(2, "设备隔离中"), UNREGISTER(3, "设备未注册");

        TencentDeviceState(int key, String value) {
            this.key = key;
            this.value = value;
        }

        public int getKey() {
            return key;
        }

        public String getValue() {
            return value;
        }

        private int key;

        private String value;
    }

    /**
     * 返回http请求对象
     *
     * @return IotvideoindustryClient.class
     */
    protected IotvideoindustryClient httpClient() throws NewlyDeviceException {
        try {
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(host);
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            IotvideoindustryClient client = new IotvideoindustryClient(cred, region, clientProfile);
            return client;
        } catch (Exception e) {
            log.error("[secretId = {} , secretKey = {} , host = {} , region = {}]"
                    , secretId, secretKey, host, region);
            throw new NewlyDeviceException("获取请求对象失败: ", e);
        }
    }

    /**
     * 得到全天时间模板
     *
     * @return TXTimeTemplateItem.class
     */
    protected TXTimeTemplateItem getFullTimeTemplate() {
        // 获取录制时间模板
        TXQueryTimeTempResp txQueryTimeTempResp = tencentRecord.queryTimeTemplate();
        TXTimeTemplateItem template = null;
        for (TXTimeTemplateItem item : txQueryTimeTempResp.getTemplates()) {
            if (item.getTemplateId().equals(this.timeTemplateId)) {
                template = item;
                break;
            }
        }
        // 找不到全天时间模板需要创建
        if (ObjectUtil.isEmpty(template)) {
            TXCreateTimeTempReq req = new TXCreateTimeTempReq();
            req.setName("全时间段模板");
            req.setIsFoolTime(1l);
            TXCreateTimeTempResp resp = tencentRecord.createTimeTemplate(req);
            if (!ObjectUtil.isEmpty(resp) && !StrUtil.isBlank(resp.getTemplateId())) {
                template = new TXTimeTemplateItem();
                template.setTemplateId(resp.getTemplateId());
                template.setName(req.getName());
                template.setIsFullTime(req.getIsFoolTime());
            }
        }
        return template;
    }
}
